package com.xxx.system.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.system.domain.Dict;
import com.xxx.system.query.DictQuery;
import com.xxx.system.service.IDictService;
import com.xxx.user.domain.User;
import com.xxx.user.query.UserQuery;
import com.xxx.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author BoWei Wang
 * @date 2025/8/5
 */

@RestController
@RequestMapping("/dict")
@Api(tags = "字典接口类")
public class DictController {
    @Autowired
    private IDictService dictService;

    @GetMapping
    @ApiOperation("查询所有字典")
    public R<List<Dict>> findAll() {
        return R.ok(dictService.findAll());
    }

    @GetMapping("/{id}")
    @ApiOperation("查询单个字典")
    public R<Dict> findOne(@PathVariable Long id) {
        return R.ok(dictService.findOne(id));
    }

    @PostMapping
    @ApiOperation("分页条件查询字典")
    public PageInfo<Dict> queryPage(@RequestBody DictQuery dictQuery) {
        List<Dict> dicts = dictService.queryPage(dictQuery);
        return new PageInfo<Dict>(dicts);
    }

    @PostMapping("/add")
    @ApiOperation("添加字典")
    public R<Void> addDict(@RequestBody Dict dict) {
        dictService.addDict(dict);
        return R.ok();
    }

    @PutMapping
    @ApiOperation("新增修改字典")
    public R<Void> addOrUpdateDict(@RequestBody Dict dict) {
        try {
            if (dict.getId() == null) {
                dictService.addDict(dict);
            } else {
                dictService.updateDict(dict);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("字典名已存在!");
        }
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除字典")
    public R<Void> deleteDict(@PathVariable Long id) {
        try {
            dictService.deleteDict(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @PatchMapping
    @ApiOperation("批量删除字典")
    public R<Void> batchDeleteDicts(@RequestBody Long[] ids) {
        try {
            dictService.batchDeleteDicts(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

}
